package edu.ntu.cloudroid.model.policy;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import edu.ntu.cloudroid.model.IDs;
import edu.ntu.cloudroid.model.UserData;
import edu.ntu.cloudroid.xacml.builder.policy.AbstractPolicyBuilder;
import edu.ntu.cloudroid.xacml.builder.policy.SimpleSlidingWindowBuilder;
import edu.ntu.cloudroid.xacml.obligation.ObligationHandle;
import edu.ntu.cloudroid.xacml.obligation.SlidingWindowObligation;

@PersistenceCapable(detachable = "true")
@Inheritance
public class SimpleSlidingWindow extends AbstractPrivacyPreference {
	@Persistent
	private int summaryType;

	@Persistent
	private int windowSize; // in number of intervals

	@Persistent
	private Date startingTime;

	@Persistent
	private int advanceStep;

	@Override
	public void initializePreference(String[] values) throws Exception {
		// <average|frequent> (<size> <starting> <advance>) or (<size>)
		int i = 0;
		this.summaryType = new Integer(values[i]).intValue();
		i++;
		this.setDataColumnIndex(new Integer(values[i]).intValue());
		i++;

		this.windowSize = new Integer(values[i]).intValue();
		i++;
		this.startingTime = new SimpleDateFormat("yyyy-MM-dd HH:mm")
				.parse(values[i]);
		i++;
		this.advanceStep = new Integer(values[i]).intValue();
		i++;

	}

	public int getFunctionId() {
		return this.summaryType;
	}

	@Override
	public AbstractPolicyBuilder getPolicyBuilder() {
		return new SimpleSlidingWindowBuilder();
	}


	public int getWindowSize() {
		return windowSize;
	}

	public void setWindowSize(int windowSize) {
		this.windowSize = windowSize;
	}

	public Date getStartingTime() {
		return startingTime;
	}

	public void setStartingTime(Date startingTime) {
		this.startingTime = startingTime;
	}

	public int getAdvanceStep() {
		return advanceStep;
	}

	public void setAdvanceStep(int advanceStep) {
		this.advanceStep = advanceStep;
	}

	@Override
	public UserData getMetaData(Object param) throws Exception {
		String type = "";
		if (param == null)
			type = type + AbstractPrivacyPreference.ISALL;
		else
			type = type + AbstractPrivacyPreference.ISGROUP + ","
					+ param.toString();
		new SimpleDateFormat("yyyy-MM-dd HH:mm").format(this.getStartingTime());

		String s = this.getUserName()
				+ ","
				+ IDs.PolicyID.SimpleSlidingWindow.ordinal()
				+ ","
				+ type
				+ ","
				+ this.getFunctionId()
				+ ","
				+ this.getDataColumnIndex()
				+ ","
				+ this.getWindowSize()
				+ ","
				+ new SimpleDateFormat("yyyy-MM-dd HH:mm").format(this
						.getStartingTime()) + "," + this.getAdvanceStep() + ","
				+ this.isLatestDataOnly();

		return new PolicyInfo(this.getUserName(), s.split(","));
	}

}
